from.__init__ import BaseDao
import hashlib
from __init__ import  BaseDao

class UserDao(BaseDao):
    def login(self,name,password):
        # 使用hashlib库下的md5算法将明文编码成密文
        pwd = hashlib.md5(password.encode('utf-8')).hexdigest()

        result = super().find_all('user', ' where name=%s and password=%s', name, pwd)
        if result:
            return result[0]

    def save(self, **data):
        # 外部验证数据的完整性
        data['password'] = hashlib.md5(data['password'].encode('utf-8')).hexdigest()
        return super().save('user',**data)

    def update(self,**data):
        if not data.get('id'):
            return False
        id = data.pop('id')
        if data.get('password'):
            data['password'] == hashlib.md5(data.get('password').encode('utf-8')).hexdigest()

        return super().update('user',id,**data)


if __name__ == '__main__':
    # 1.验证是否能连接成功
    dao = UserDao()
    # user = dao.login('罗鹏', '123456')
    # print(user)  #  {'id': 1, 'number': '110299199301054568', 'name': '罗鹏', 'phone': '17866542319', 'password': 'e10adc3949ba59abbe56e057f20f883e'}

    # 2.存储数据
    # user1 = dict(name='disen',
    #             password='1234',
    #             number = '13575414253')
    # result = dao.save(**user1)
    # print(result)

    # 3.更新数据
    update_user = dict(id=7, number="45312312321")
    print(dao.update(**update_user))

